package com.dayu.recovery.service.data;

import com.dayu.recovery.domain.data.GoodsTimeData;
import com.dayu.recovery.mapper.GoodsTimeDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class GoodsTimeDataService {

    @Autowired
    private GoodsTimeDataMapper goodsTimeDataMapper;

    /**
     * 基础时效列表
     * @param timeData
     * @return
     */
    public List<GoodsTimeData> list(GoodsTimeData timeData) {
        return goodsTimeDataMapper.selectGoodsTimeDataList(timeData);
    }

    public List<GoodsTimeData> onlyList(GoodsTimeData timeData) {
        List<GoodsTimeData> list = goodsTimeDataMapper.selectGoodsTimeDataList(timeData);
        Set<String> set = new HashSet<>();
        //中转
        List<GoodsTimeData> only = new ArrayList<>();
        //返回结果
        List<GoodsTimeData> res = new ArrayList<>();
        for(GoodsTimeData data : list) {
            if(set.add(data.getGoodsId())) {
                if(only.size() > 0) {
                    //构建数据
                    GoodsTimeData d = new GoodsTimeData();
                    d.builderByList(only);
                    res.add(d);
                    //清空
                    only = new ArrayList<>();
                }
            }
            only.add(data);
        }

        if(only.size() > 0) {
            GoodsTimeData d = new GoodsTimeData();
            d.builderByList(only);
            res.add(d);
        }
        return res;
    }
}
